package com.wiim.adapter;

import java.util.ArrayList;

import com.wiim.common.ConstantValue;
import com.wiim.common.Utilities;
import com.wiim.model.BannerModel;
import com.wiim.model.PageModel;
import com.wiim.nhapnhap.CustomFragmentActivity;
import com.wiim.nhapnhap.R;

import android.app.Activity;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class PageAdapter extends PagerAdapter {

	static class ViewHolder {
	}

	private ArrayList<PageModel> listBanner;
	private LayoutInflater inflater;
	private ImageView imgBanner;
	private Activity act;
	private TextView tvNameStore;

	public PageAdapter(final Activity _act, ArrayList<PageModel> _listBanner) {
		this.listBanner = _listBanner;
		this.inflater = _act.getLayoutInflater();
		act = _act;
	}

	@Override
	public void destroyItem(final View container, final int position, final Object object) {
		((ViewPager) container).removeView((View) object);
	}

	@Override
	public void finishUpdate(final View container) {
	}

	@Override
	public int getCount() {
		return this.listBanner.size();
	}

	@Override
	public Object instantiateItem(final View view, final int position) {

		final View imageLayout = this.inflater.inflate(R.layout.item_page_layout, null);
		imgBanner = (ImageView) imageLayout.findViewById(R.id.imgBanner);
		tvNameStore = (TextView) imageLayout.findViewById(R.id.tvNameStore);
		((ViewPager) view).addView(imageLayout, 0);
//		ImageLoader.getInstance(act).DisplayImage(listBanner.get(position).getUrlImageBanner().toString(), imgBanner, false, Utilities.getGlobalVariable(activity).clearCase);
		imgBanner.setBackgroundResource(listBanner.get(position).getImagestore());
		tvNameStore.setText(listBanner.get(position).getNamestore());
		imgBanner.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				((CustomFragmentActivity)act).replaceFragment(ConstantValue.STORE_DETAIL_FRAGMENT, false);
			}
		});
//		if (position == listBanner.size() - 1) {
//			activity.hideProgressBar();
//		}
		return imageLayout;
	}

	@Override
	public boolean isViewFromObject(final View view, final Object object) {
		return view.equals(object);
	}

	@Override
	public void restoreState(final Parcelable state, final ClassLoader loader) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(final View container) {
	}

	public void recycleImg() {
		Utilities.recycle(imgBanner);
	}

}
